package com.gaoyi.crm.fragment;

import android.Manifest;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import androidx.activity.result.ActivityResult;
import androidx.activity.result.ActivityResultLauncher;
import androidx.navigation.Navigation;

import com.gaoyi.crm.R;
import com.gaoyi.crm.callback.IView;
import com.gaoyi.crm.constant.ApiStatus;
import com.gaoyi.crm.constant.BundleKeyName;
import com.gaoyi.crm.constant.RequestCode;
import com.gaoyi.crm.control.dialog.LoadingDialogUtils;
import com.gaoyi.crm.databinding.FragmentScanDoneBinding;
import com.gaoyi.crm.fragment.base.BaseFragment;
import com.gaoyi.crm.util.PermissionUtils;
import com.gaoyi.crm.util.ToastUtil;
import com.gaoyi.crm.util.Utils;
import com.gaoyi.crm.vm.ScanDoneViewModel;
import com.gs.zxinglibrary.zxing.activity.CodeUtils;

import java.util.Map;

public class ScanDoneFragment extends BaseFragment<FragmentScanDoneBinding, ScanDoneViewModel> implements View.OnClickListener, IView {

    private ActivityResultLauncher<Intent> mScanResult;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_scan_done;
    }

    @Override
    protected void initView() {
        mPermissionsResult = registerForPermissionsResult();
        mScanResult = registerForActivityResult(RequestCode.REQUEST_SCAN);
        mBinding.btnScanNext.setOnClickListener(this);
        mBinding.btnDone.setOnClickListener(this);
    }

    @Override
    protected void initLiveData() {
        mViewModel.getResultCode().observe(getViewLifecycleOwner(),integer -> {
            if (integer == ApiStatus.FAIL) {
                toHomePage();
            }
        });
    }

    private void toScan() {
        Utils.scan(mContext, mScanResult, 1);
    }

    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.btnDone) {
            toHomePage();
//            Navigation.findNavController(view).navigate(R.id.action_scanDoneFragment_to_homePageFragment);
        } else {
            scanNext();
        }
    }

    private void toHomePage() {
        Utils.backToHomePage(mController);
    }

    @Override
    protected void initData() {
        Bundle bundle = getArguments();
        if (bundle == null) {
            return;
        }
        String itemNo = bundle.getString(BundleKeyName.RESULT);
        mBinding.tvItemNo.setText(getString(R.string.scan_done_title, itemNo));
        mViewModel.getItemNoStore(itemNo);
    }

    public void scanNext() {
        boolean permission = PermissionUtils.addPermission(requireActivity(), new String[]{
                Manifest.permission.CAMERA,
                Manifest.permission.WRITE_EXTERNAL_STORAGE
        }, mPermissionsResult); // 判断是否有权限
        if (permission) {
            toScan();
        }
    }

    @Override
    protected void onRequestPermissionsResult(Map<String, Boolean> result) {
        if (PermissionUtils.requestPermissionResult(result, Manifest.permission.CAMERA)
                && PermissionUtils.requestPermissionResult(result, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
            toScan();
        } else {
            ToastUtil.showToast(R.string.no_permission);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, ActivityResult result) {
        if (requestCode == RequestCode.REQUEST_SCAN) {
            scanResult(result);
        }
    }

    private void scanResult(ActivityResult result) {
        Intent data = result.getData();
        if (result.getResultCode() == 0 || data == null) {
            return;
        }
        Bundle dataExtras = data.getExtras();
        if (dataExtras == null) {
            return;
        }
        if (dataExtras.getInt(CodeUtils.RESULT_TYPE) == CodeUtils.RESULT_SUCCESS) {
            String itemNo = dataExtras.getString(CodeUtils.RESULT_STRING);
            mBinding.tvItemNo.setText(getString(R.string.scan_done_title, itemNo));
            mViewModel.getItemNoStore(itemNo);
        }
    }

    @Override
    public void showLoading() {
        LoadingDialogUtils.getInstance().showLoading(mContext);
    }

    @Override
    public void hideLoading() {
        LoadingDialogUtils.getInstance().dismiss();
    }
}